import { showToast } from 'vant'
import APIS from '../api/api'
import http from '../api/request'

const userService = {
  rules: {
    userTel: {
      rule: /^1[23456789]\d{9}$/,
      msg: '手机输入内容错误，请重新输入',
    },
    userPwd: {
      rule: /^\w{6,12}$/,
      msg: '密码输入内容错误，请重新输入',
    },
  },
  getUserInfo(params) {
    return $axios.post(APIS.userLogin, params)
  },
  validateUserTel(userTel) {
    if (!this.rules.userTel.rule.test(userTel)) {
      showToast(this.rules.userTel.msg)
      return false
    }
    return true
  },
  validateUserPwd(userPwd) {
    if (!this.rules.userPwd.rule.test(userPwd)) {
      showToast(this.rules.userPwd.msg)
      return false
    }
    return true
  },
  async login(account, password) {
    return await http.$axios({
      url: APIS.userLogin,
      method: 'POST',
      params: { userAccount: account, userPassword: password },
    })
  },
  async register(data = {}) {
    return await http.$axios({
      url: APIS.userRegister,
      method: 'POST',
      data: data,
    })
  },
  async modify(account, newPassword) {
    return await http.$axios({
      url: APIS.modifyPassword,
      method: 'POST',
      data: {
        userAccount: account,
        userPassword: newPassword,
      },
    })
  },
}

export default userService
